package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.InspectionItemParam;
import com.mxpio.erp.common.quality.service.InspectionItemParamService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InspectionItemParamController", description = "检测项目-参数选项接口")
@RestController
@RequestMapping("/erp/quality/ii/param/")
public class InspectionItemParamController {

    @Autowired
    private InspectionItemParamService lineService;

    @GetMapping("list")
    @Operation(summary = "检测项目-参数选项列表", description = "获取检测项目-参数选项列表（无分页）", method = "GET")
    public Result<List<InspectionItemParam>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<InspectionItemParam> lines = lineService.list(InspectionItemParam.class, criteria);
        return Result.OK(lines);
    }

    @GetMapping("list/{code}")
    @Operation(summary = "根据code获取检测项目-参数选项列表", description = "根据code获取获取检测项目-参数选项列表（无分页）", method = "GET")
    public Result<List<InspectionItemParam>> listByBizNo(@Parameter(description="方案编号") @PathVariable(name = "code", required = true) String code) throws UnsupportedEncodingException {
        //Criteria c = Criteria.create().addCriterion("schemeCode", Operator.EQ, code);
        List<InspectionItemParam> lines = lineService.listByCode(code);
        return Result.OK(lines);
    }

    @GetMapping("page")
    @Operation(summary = "检测项目-参数选项列表", description = "获取检测项目-参数选项列表（分页）", method = "GET")
    public Result<Page<InspectionItemParam>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<InspectionItemParam> lines = lineService.listPage(InspectionItemParam.class, page, criteria);
        return Result.OK(lines);
    }


    @PostMapping("add")
    @Operation(summary = "新增检测项目-参数选项", description = "新增检测项目-参数选项", method = "POST")
    public Result<InspectionItemParam> add(@RequestBody InspectionItemParam inspectionSchemeLine) {
        lineService.save(inspectionSchemeLine);
        return Result.OK(inspectionSchemeLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑检测项目-参数选项", description = "编辑检测项目-参数选项", method = "PUT")
    public Result<InspectionItemParam> edit(@RequestBody InspectionItemParam inspectionSchemeLine) {
        lineService.update(inspectionSchemeLine);
        return Result.OK(inspectionSchemeLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除检测项目-参数选项", description = "删除检测项目-参数选项,多个id用英文逗号分隔", method = "DELETE")
    @Transactional
    public Result<InspectionItemParam> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            lineService.delete(InspectionItemParam.class, key);
        }
        return Result.OK();
    }
}
